﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

[ApiController]
[Route("api/orders")] 
public class OrdersController : ControllerBase
{
    private readonly OrderService _orderService;

    public OrdersController(OrderService orderService)
    {
        _orderService = orderService;
    }

    // GET: api/orders
    [HttpGet]
    public ActionResult<IEnumerable<Order>> GetAllOrders()
    {
        try
        {
            var orders = _orderService.GetAllOrders();
            return Ok(orders);
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Internal server error: {ex.Message}");
        }
    }

    // GET: api/orders/search
    [HttpGet("search")] 
    public ActionResult<IEnumerable<Order>> SearchOrders(
        [FromQuery] int? id,
        [FromQuery] string customer,
        [FromQuery] decimal? minTotal,
        [FromQuery] decimal? maxTotal)
    {
        try
        {
            var parameters = new OrderService.OrderQueryParameters
            {
                OrderId = id,
                Customer = customer,
                MinAmount = minTotal,
                MaxAmount = maxTotal
            };

            var orders = _orderService.QueryOrders(parameters);
            return Ok(orders);
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Internal server error: {ex.Message}");
        }
    }

    // GET: api/orders/{id}
    [HttpGet("{id}")]
    public ActionResult<Order> GetOrderById(int id)
    {
        try
        {
            var parameters = new OrderService.OrderQueryParameters { OrderId = id };
            var orders = _orderService.QueryOrders(parameters);

            if (orders.Count == 0)
            {
                return NotFound($"Order with ID {id} not found");
            }

            return Ok(orders[0]);
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Internal server error: {ex.Message}");
        }
    }

    // POST: api/orders
    [HttpPost]
    public ActionResult<Order> CreateNewOrder([FromBody] Order order)
    {
        try
        {
            _orderService.AddOrder(order);
            return CreatedAtAction(nameof(GetOrderById), new { id = order.OrderId }, order);
        }
        catch (InvalidOperationException ex)
        {
            return Conflict(ex.Message);
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Internal server error: {ex.Message}");
        }
    }


    // DELETE: api/orders/{id}
    [HttpDelete("{id}")]
    public IActionResult DeleteOrderById(int id)
    {
        try
        {
            _orderService.RemoveOrder(id);
            return NoContent();
        }
        catch (InvalidOperationException ex)
        {
            return NotFound(ex.Message);
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Internal server error: {ex.Message}");
        }
    }
}